package ru.ccfit.nsu.g7204.dronov.adPlatform;

import javax.swing.*;
import java.awt.event.*;

/**
 * Created by IntelliJ IDEA.
 * User: Drongo
 * Date: 22.03.11
 * Time: 16:28
 * To change this template use File | Settings | File Templates.
 */
public class FeatureChoseForm{
    private JList featureList;
    private JPanel panel;
    private JButton cancelButton;
    private JButton okButton;
    private JFrame form;
    private FeatureChoseFormListener listener;
    private  int i;
    public void setListener(FeatureChoseFormListener listener) {
        this.listener = listener;
    }

    private class ItemValueHandleTask implements Runnable
    {
        Object itemVal;
        private ItemValueHandleTask(Object itemVal) {
            this.itemVal = itemVal;
        }

        public void run()
        {
            if (itemVal==null || listener==null)
                return;
            Feature f=(Feature)itemVal;
            listener.setSelected(f);
            form.dispose();
        }
    }

    public FeatureChoseForm(Object[] data) {
        form=new JFrame();
        form.setContentPane(panel);
        featureList.setListData(data);
        form.setLocation(500,500);
        form.setSize(300,300);
        form.setVisible(true);
        i=0;
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               form.dispose();
            }
        });

        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        SwingUtilities.invokeLater(new ItemValueHandleTask(featureList.getSelectedValue()));
                    }
                });

            }
        });

        featureList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (e.getClickCount() == 2)
                {
                    int index = featureList.locationToIndex(e.getPoint());
                    SwingUtilities.invokeLater(new ItemValueHandleTask(featureList.getModel().getElementAt(index)));
                }
            }
        });

        form.addWindowFocusListener(new WindowFocusListener() {
            public void windowGainedFocus(WindowEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            public void windowLostFocus(WindowEvent e) {
               e.getWindow().toFront();
            }
        });

    }
}
